package com.sanjiahantang.youjiarole.module.login.models.impl;

import android.content.Context;

import com.sanjiahantang.youjiarole.base.BaseBean;
import com.sanjiahantang.youjiarole.base.CallBack;
import com.sanjiahantang.youjiarole.config.ConfigValue;
import com.sanjiahantang.youjiarole.http.BaseDelegate;
import com.sanjiahantang.youjiarole.http.OkHttpClientManager;
import com.sanjiahantang.youjiarole.module.common.beans.StringBean;
import com.sanjiahantang.youjiarole.module.login.beans.LoginBean;
import com.sanjiahantang.youjiarole.module.login.models.ILoginModel;
import com.squareup.okhttp.Request;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by zy on 2017/10/21.
 */

public class LoginModel implements ILoginModel {
    @Override
    public void doLogin(final Context context, Map<String, String> params, final CallBack<LoginBean> callBack) {

        Map<String, Object> pa = new HashMap<>();
        pa.put("phone",params.get("phone"));
        pa.put("password",params.get("password"));
        OkHttpClientManager.postAsyn(context, ConfigValue.API_URL
                + "/user/phone/role/loginForPassword", params, new BaseDelegate.ResultCallback<LoginBean>() {
            @Override
            public void onError(Request request, Object tag, Exception e) {
                callBack.onFailure(e.getMessage());
            }

            @Override
            public void onResponse(LoginBean response, Object tag) {
                callBack.onSuccess(response, context, tag);
            }
        });
    }

    @Override
    public void doRegister(final Context context, Map<String, String> params, final CallBack<StringBean> callBack) {
        OkHttpClientManager.postAsyn(context, ConfigValue.API_URL + "/user/phone/role/registerCustomer",
                params, new BaseDelegate.ResultCallback<StringBean>() {
                    @Override
                    public void onError(com.squareup.okhttp.Request request, Object tag, Exception e) {
                        callBack.onFailure(e.getMessage());
                    }

                    @Override
                    public void onResponse(StringBean response, Object tag) {
                        callBack.onSuccess(response,context,tag);
                    }
                },params);
    }

    @Override
    public void doChangePassword(final Context context, Map<String, Object> params,final CallBack<BaseBean> callBack) {

        OkHttpClientManager.postJson(context, ConfigValue.API_URL + "/user/phone/role/modifyPwd",
                params, new BaseDelegate.ResultCallback<BaseBean>() {
                    @Override
                    public void onError(Request request, Object tag, Exception e) {
                        callBack.onFailure(e.getMessage());
                    }

                    @Override
                    public void onResponse(BaseBean response, Object tag) {
                        callBack.onSuccess(response,context,tag);
                    }
                });
    }

    //发送验证码成功
    @Override
    public void senMessageCode(final Context context, Map<String, String> params, final CallBack<StringBean> callBack) {

        OkHttpClientManager.postAsyn(context, ConfigValue.API_URL + "/user/phone/role/getRegisterCustomerIdentifyingCode",
                params, new BaseDelegate.ResultCallback<StringBean>() {
                    @Override
                    public void onError(Request request, Object tag, Exception e) {
                        callBack.onFailure(e.getMessage());
                    }

                    @Override
                    public void onResponse(StringBean response, Object tag) {
                        callBack.onSuccess(response,context,tag);
                    }
                },true);
    }

    @Override
    public void doforgetPassword(final Context context, Map<String, String> params,final CallBack<StringBean> callBack) {

        OkHttpClientManager.postAsyn(context, ConfigValue.API_URL + "/user/phone/role/findPassword",
                params, new BaseDelegate.ResultCallback<StringBean>() {
                    @Override
                    public void onError(Request request, Object tag, Exception e) {
                        callBack.onFailure(e.getMessage());
                    }

                    @Override
                    public void onResponse(StringBean response, Object tag) {
                        callBack.onSuccess(response,context,tag);
                    }
                },true);
    }

    @Override
    public void setForgetSendCodeView(final Context context, Map<String, String> params, final CallBack<StringBean> callBack) {
        OkHttpClientManager.postAsyn(context, ConfigValue.API_URL + "/user/phone/role/getRegisterCustomerPassWordCode",
                params, new BaseDelegate.ResultCallback<StringBean>() {
                    @Override
                    public void onError(Request request, Object tag, Exception e) {
                        callBack.onFailure(e.getMessage());
                    }

                    @Override
                    public void onResponse(StringBean response, Object tag) {
                        callBack.onSuccess(response,context,tag);
                    }
                },true);
    }

    @Override
    public void modifyPayPwd(final Context context, Map<String, Object> params, final CallBack<StringBean> callBack) {
        OkHttpClientManager.postJson(context, ConfigValue.API_URL + "/user/phone/role/modifyPayPwd",
                params, new BaseDelegate.ResultCallback<StringBean>() {
                    @Override
                    public void onError(Request request, Object tag, Exception e) {
                        callBack.onFailure(e.getMessage());
                    }

                    @Override
                    public void onResponse(StringBean response, Object tag) {
                        callBack.onSuccess(response, context, tag);
                    }
                });
    }
}
